﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace HMS
{
    public partial class ucDiadiem : UserControl
    {  
        
        private SqlConnection con = new SqlConnection(FRM_Manhinhchinh.sConn);
        public static SqlDataAdapter dataAp;
        public static DataTable dataTable;
        private string cityID;
        ucQuanhuyen frm;
        public ucDiadiem()
        {
            InitializeComponent();
            SqlConnection con = new SqlConnection(FRM_Manhinhchinh.sConn);
            SqlDataAdapter da = new SqlDataAdapter("Select city_name, city_id from tblcity", con);
            DataTable tb = new DataTable();
            tb.Columns.Add("city_id", typeof(string));
            tb.Columns.Add("city_name", typeof(string));
            tb.Rows.Add("0","Tất Cả");

            da.Fill(tb);
            cmbTinhthanh.DataSource = tb;
            cmbTinhthanh.DisplayMember = "City_name";
            cmbTinhthanh.ValueMember = "city_id";
        }
      
        private void rdbXemquanhuyen_CheckedChanged(object sender, EventArgs e)
        {
            ViewDistrict();
        }

        private void ViewDistrict()
        {
            if (rdbXemquanhuyen.Checked == true)
            {
                rdbXemtinhthanh.Checked = false;
                cmbTinhthanh.Enabled = true;
            }
            pcDulieu.Controls.Clear();
            frm = new ucQuanhuyen();
            frm.CityID = cityID;
            frm.Dock = System.Windows.Forms.DockStyle.Fill;
            pcDulieu.Controls.Add(frm);
        }
        private bool controlKey = false;
        private void cmbTinhthanh_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (e.KeyChar == (int)Keys.Escape)
            {
                cmbTinhthanh.SelectedIndex = -1;
                cmbTinhthanh.Text = "";
                controlKey = true;
            }
            else
            {
                if (Char.IsControl(e.KeyChar))
                {
                    controlKey = true;
                }
                else
                {
                    controlKey = false;
                }
            }
        }

        private void cmbTinhthanh_TextChanged(object sender, EventArgs e)
        {
            base.OnTextChanged(e);
            if (cmbTinhthanh.Text != "" && !controlKey)
            {
                string matchtext = cmbTinhthanh.Text;
                int match = cmbTinhthanh.FindString(matchtext);
                if (match != -1)
                {
                    cmbTinhthanh.SelectedIndex = match;
                    cmbTinhthanh.SelectionStart = matchtext.Length;
                    cmbTinhthanh.SelectionLength = cmbTinhthanh.Text.Length - cmbTinhthanh.SelectionStart;
                }
            }
        }

        private void rdbXemtinhthanh_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbXemtinhthanh.Checked == true)
            {
                rdbXemquanhuyen.Checked = false;
                cmbTinhthanh.Enabled = false;
            }

            pcDulieu.Controls.Clear();
            ucTinhthanh frm = new ucTinhthanh();
            frm.Dock = System.Windows.Forms.DockStyle.Fill;
            pcDulieu.Controls.Add(frm);
        }
         
         public DataTable GetDataTable(string sql)
        {
            // Tạo dataApdapter, thực hiện câu lệnh query
            dataAp = new SqlDataAdapter(sql, con);
            // Đổ dữ liệu vào DataTable
            dataTable = new DataTable();
            dataAp.Fill(dataTable);
            return dataTable;
        }

         private void cmbTinhthanh_SelectedIndexChanged(object sender, EventArgs e)
         {
             if (rdbXemquanhuyen.Checked == true)
             {
                 cityID = cmbTinhthanh.SelectedValue.ToString();
                 
                 frm.CityID = cityID;
                 ViewDistrict();
             }
             
         }
            
        
       

    }
}


